<?php
/**
 * Simple méthode d'affichage…
 *
 * @version 1.0
 * @author jonathan <jo.link.noir@gmail.com>
 * @date 30-08-2010
 */
class U1_Render
{
	static private
		$_planets = array(),
		$_players = array(),
		$_resources = array();


	static public function addPlayer($idPlayer, U1_Player $o = null)
	{
		self::$_players[$idPlayer] = $o ? $o : false;
	}

	static public function addPlanet($idPlanet, SW_Planet_Element $o = null)
	{
		self::$_planets[$idPlanet] = $o ? $o : false;
	}

	static public function addResource($idPlanet, U1_Resource $o = null)
	{
		self::$_resources[$idPlanet] = $o ? $o : false;
	}

	/**
	 * Affiche quelque chose de la forme <{$name}s><{$name} group="$g">$v</{$name}>…</{$name}s>'
	 * @param string $name : nom du nœud pour le contener. Les valeurs de $contenerGroup seront entouré d'un nœud du même nom que $name avec un s final
	 * @param Traversable|array $contenerGroups
	 */
	final static public function blocNodeGroup($name, $contenerGroups)
	{
		$r =  '<'.$name.'s>';
		foreach ($contenerGroups as $g => $v)
			$r .= '<'.$name.' group="'.$g.'">'.$v.'</'.$name.'>';
		return $r.'</'.$name.'s>';
	}

	/**
	 * @param array $coordinates
	 */
	final static public function coordinates(array $coordinates)
	{
		return '<coordinates>
			<galaxy>'.$coordinates[0].'</galaxy>
			<system>'.$coordinates[1].'</system>
			<position>'.$coordinates[2].'</position>
		</coordinates>';
	}

	/**
	 * Retourne un interval au format PnYnMnDTnHnMnS avec Y et M(mois) ignoré, les chiffres (n) sur 2 chiffres et les seconde toujours présentent
	 * @param int $time
	 * @return string
	 */
	static public function interval($time)
	{
		$s = 'P';
		self::intervalComposant($s, $time, 86400, 'D');
		$s .= 'T';
		self::intervalComposant($s, $time, 3600, 'H');
		self::intervalComposant($s, $time, 60, 'M');
		return $s.($time < 10 ? '0'.$time : $time).'S';
	}

	static private function intervalComposant(&$s, &$time, $m, $u)
	{
		$tmp = $time % $m;
		if ($tmp < $time)
		{
			$i = ($time - $tmp) / $m;
			$s .= ($i < 10 ? '0'.$i : $i).$u;
			$time = $tmp;
		}
	}

	final static public function render()
	{
		return self::resources().self::planets().self::players();
	}

	final static private function players()
	{
		$r = '<players>';
		foreach (self::$_players as $id => $player)
		{
			if (!$player)
				$player = U1_DAO_User::select($id);
			$r .= '<player id="'.$id.'" race="'.$player->getRace().'" alliance="'.$player->getIdClan().'">'.$player->getName().'</player>';
		}
		return $r.'</players>';
	}

	final static private function planets()
	{
		$r = '<planets>';
		foreach (self::$_planets as $id => $planet)
		{
			if (!$planet)
				$planet = U1_DAO_Planet::selectId($id);
			$r .= '<planet id="'.$id.'"><name>'.$planet->getName().'</name>'.self::coordinates($planet->getTarget()).'</planet>';

			if (!isset(self::$_players[$planet->getRefUser()]))
				self::$_players[$planet->getRefUser()] = false;
		}
		return $r.'</planets>';
	}

	final static private function resources()
	{
		$r = '<resources>';
		foreach (self::$_resources as $idPlanet => $resources)
		{
			if (!$resources)
				$resources = U1_Planet::factoryContainsPlanetId($idPlanet)->getInstance(U1_Resource::INDEX, $idPlanet);

			$r .= '<resources id-planet="'.$idPlanet.'">';
			foreach ($resources->getGroups() as $group => $elements)
			{
				$r .= '<resource group="'.$group.'">';
				foreach ($elements as $element)
					$r .= '<quantity per-hour="'.$element->getPerHour().'">'.$element.'</quantity>';
				$r .= '</resource>';
			}
			$r .= '</resources>';
		}
		return $r.'</resources>';
	}
}